package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pinda.DTO.angency.AgencyScopeDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.entity.PdAgencyScope;
import com.itheima.pinda.mapper.PdAgencyScopMapper;
import com.itheima.pinda.mapper.PdCourierScopMapper;
import com.itheima.pinda.service.PdAgencyScopeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdAgencyScopeServiceImpl implements PdAgencyScopeService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdAgencyScopMapper agencyScopMapper;
    @Autowired
    private PdCourierScopMapper courierScopMapper;

    @Override
    public void batchSaveAgencyScope(List<AgencyScopeDto> dtoList) {
        dtoList.stream().forEach(agencyScopeDto -> {
            PdAgencyScope agencyScope = new PdAgencyScope();
            BeanUtils.copyProperties(agencyScopeDto,agencyScope);
            agencyScope.setId(idGenerator.nextId(agencyScope) + "");
            agencyScopMapper.insert(agencyScope);
        });
    }

    @Override
    public void deleteAgencyScope(AgencyScopeDto dto) {
        agencyScopMapper.delete(
                new LambdaQueryWrapper<PdAgencyScope>()
                        .eq(!StringUtils.isEmpty(dto.getAgencyId()),PdAgencyScope::getAgencyId,dto.getAgencyId())
                        .eq(!StringUtils.isEmpty(dto.getAreaId()),PdAgencyScope::getAreaId,dto.getAreaId()));
    }

    @Override
    public List<AgencyScopeDto> findAllAgencyScope(String areaId, String agencyId, List<String> agencyIds, List<String> areaIds) {
        List<PdAgencyScope> agencyScopes = agencyScopMapper.selectList(new LambdaQueryWrapper<PdAgencyScope>()
                .eq(!StringUtils.isEmpty(areaId), PdAgencyScope::getAreaId, areaId)
                .eq(!StringUtils.isEmpty(agencyId), PdAgencyScope::getAgencyId, agencyId)
                .in(!agencyIds.isEmpty(), PdAgencyScope::getAreaId, agencyIds)
                .in(!areaIds.isEmpty(), PdAgencyScope::getAreaId, areaIds));

        List<AgencyScopeDto> collect = agencyScopes.stream().map(pdAgencyScope -> {
            AgencyScopeDto agencyScopeDto = new AgencyScopeDto();
            BeanUtils.copyProperties(pdAgencyScope, agencyScopeDto);
            return agencyScopeDto;
        }).collect(Collectors.toList());

        return collect;
    }
}
